package connManage

import (
	"chart.zozoo.net/app/models"
	"chart.zozoo.net/app/service/usersOnlineService"
	"github.com/gorilla/websocket"
)

type ConnInfo struct {
	UserInfo *models.Users
	Conn     *websocket.Conn
}

// LocalConnections 全局连接对象
var localConnections = make(map[string]*ConnInfo)

func AddConn(userInfo *models.Users, conn *websocket.Conn) {
	var addr = conn.RemoteAddr().String()
	localConnections[addr] = &ConnInfo{UserInfo: userInfo, Conn: conn}

	//处理用户在线情况
	err := usersOnlineService.Update(userInfo.Uuid, 1, addr)
	if err != nil {
		//写入错误
		return
	}
}

func RemoveConn(addr string) {

	//处理用户在线情况

	//删除连接信息
	_, ok := localConnections[addr]
	if ok {
		delete(localConnections, addr)
	}
}

func GetConn(addr string) *ConnInfo {
	conn, ok := localConnections[addr]
	if ok {
		return conn
	}

	return nil
}
